package logic

import (
	"context"
	"errors"
	"fmt"

	"zerodemo/user-rpc/internal/svc"
	"zerodemo/user-rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/metadata"
)

type GetUserInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
	return &GetUserInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserInfoLogic) GetUserInfo(in *pb.GetUserInfoReq) (*pb.GetUserInfoRes, error) {
	// todo: add your logic here and delete this line

	if md, ok := metadata.FromIncomingContext(l.ctx); ok {
		tmp := md.Get("username")
		fmt.Printf("tmp ===> %+v\r\n", tmp)
		if len(tmp) < 0 {
			return &pb.GetUserInfoRes{}, errors.New("错误的username")
		}
	}

	m := map[int64]string{
		1: "张三",
		2: "李四",
	}

	nickname := "null"

	if name, ok := m[in.Id]; ok {
		nickname = name
	}

	return &pb.GetUserInfoRes{
		Nickname: nickname,
		Id:       in.Id,
	}, nil
}
